#!/bin/sh
TOUCHPAD_ID=$(xinput | sed -n '/[Tt]ouch[Pp]ad/ {s/^.*id=//; s/[\t\s].*//; p}')
[ -z $TOUCHPAD_ID ] && exit

###
die() {
	echo "$@"
	exit 1
}

enable() {
	# currently disabled, so enable it
	xinput enable $TOUCHPAD_ID
	echo "Touchpad enabled"
}

disable() {
	# currently enabled, so disable it
	xinput disable $TOUCHPAD_ID
	echo "Touchpad disabled"
}

toggle() {
	# enabled status
	TOUCHPAD_ENABLED=$(xinput list-props 16 | sed -n "/Device Enabled/ {s/^.*://;p}")
	[ -z $TOUCHPAD_ENABLED ] && exit
	if [ $TOUCHPAD_ENABLED -eq 1 ]; then
		disable
	else
		enable
	fi 
}

### main
case $1 in
	--help|-h) die "Usage: ${0##*/} [enable|disable]" ;;
	enable|enabled)  enable ;;
	disable|disabled) disable ;;
	"") toggle ;;
esac
